import pytest
from playwright.sync_api import sync_playwright

"""
测试追踪功能
"""
@pytest.fixture(scope="function")
def chromium_browser():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False, slow_mo=200)
        yield browser
        browser.close()


def test_login(chromium_browser) -> None:
    context = chromium_browser.new_context()

    """启动追踪
    1、screenshots-跟踪记录将包含与页面相关的屏幕截图（如果在大规模测试中启用屏幕截图，可能会增加跟踪文件的大小和性能开销）
    2、snapshots-跟踪记录将包含页面快照
    3、sources-跟踪记录将包含页面的 HTML、CSS 和 JavaScript 源代码
    """
    context.tracing.start(screenshots=True, snapshots=True, sources=True)

    page = context.new_page()
    page.goto("http://127.0.0.1:34009/xxl-job-admin/toLogin")
    page.get_by_placeholder("请输入登录账号").click()
    page.get_by_placeholder("请输入登录账号").fill("admin")
    page.get_by_placeholder("请输入登录密码").click()
    page.get_by_placeholder("请输入登录密码").fill("123456")
    page.get_by_role("button", name="登录").click()

    """结束追踪
    1、文件后缀名必须是.zip
    2、查看追踪：playwright show-trace trace.zip
    """
    context.tracing.stop(path="trace.zip")

    # context 关闭的时候保存视频
    context.close()
